package com.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.core.baseDao.extend.Pager;
import com.manager.KeywordManager;
import com.manager.TextManager;
import com.model.Keyword;
import com.model.ModuleConstant;
import com.model.TextResponse;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.util.TimeUtil;


@ParentPackage("json-default")
@Results({ @Result(name = "jsonResult", type = "json") })
@Namespace("/user")
public class TextAction extends ActionSupport{
	
	@Autowired
	private KeywordManager keyManager;
	@Autowired
	private TextManager textManager;
	
	private Keyword keyword;
	private TextResponse textResp;
	
	
	public KeywordManager getKeyManager() {
		return keyManager;
	}

	public void setKeyManager(KeywordManager keyManager) {
		this.keyManager = keyManager;
	}

	public TextManager getTextManager() {
		return textManager;
	}

	public void setTextManager(TextManager textManager) {
		this.textManager = textManager;
	}

	public Keyword getKeyword() {
		return keyword;
	}

	public void setKeyword(Keyword keyword) {
		this.keyword = keyword;
	}

	public TextResponse getTextResp() {
		return textResp;
	}

	public void setTextResp(TextResponse textResp) {
		this.textResp = textResp;
	}

	public String add(){
		return "add";
	}
	
	public String addTo(){
		Map session = ActionContext.getContext().getSession();
		String token = session.get("token")+"";
		String ret = "";
		if(keyword.getId() == null){
			ret = "关键词添加成功！";
			textResp.setCreateTime(TimeUtil.getDate());
			textResp.setUpdateTime(textResp.getCreateTime());
			textResp.setToken(token);
			keyword.setToken(token);
			keyword.setModel(ModuleConstant.TEXT);
			textResp.setClick(0);
			textResp.setKeyword(keyword);
			keyword.setTextResponse(textResp);
		}else{
			ret = "关键词更新成功！";
			Keyword key = (Keyword)keyManager.findUnique("from Keyword k where k.id=? and k.token=?", new Object[]{keyword.getId(),token});
			keyword.getTextResponse().setClick(key.getTextResponse().getClick());
			keyword.getTextResponse().setCreateTime(key.getTextResponse().getCreateTime());
			keyword.getTextResponse().setUpdateTime(TimeUtil.getDate());
			keyword.getTextResponse().setKeyword(keyword);
		}
		keyManager.saveOrUpdate(keyword);
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute("ret", ret);
		return "add";
	}
	
	public String delete(){
		Map session = ActionContext.getContext().getSession();
		String token = session.get("token")+"";
		String id = ServletActionContext.getRequest().getParameter("id");
		keyword = (Keyword)keyManager.findUnique("from Keyword k where k.id=? and k.token=?", new Object[]{Integer.parseInt(id),token});
		keyManager.delete(keyword);
		return index();
	}
	
	public String index(){
		ServletRequest request = ServletActionContext.getRequest();
		Pager page = new Pager();
		String p = request.getParameter("p");
		if(!StringUtils.isEmpty(p)){
			page.setCurrPage(Integer.parseInt(p));
		}
		Map session = ActionContext.getContext().getSession();
		String token = (String)session.get("token");
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("token", token);
		param.put("model", ModuleConstant.TEXT);
		page = keyManager.findByPage("from Keyword ", param, page);
		request.setAttribute("page", page);
		return "index";
	}
	
	public String edit(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String id = request.getParameter("id");
		Map session = ActionContext.getContext().getSession();
		String token = (String)session.get("token");
		keyword = (Keyword)keyManager.findUnique("from Keyword k where k.id=? and k.token=?", new Object[]{Integer.parseInt(id),token});
		request.setAttribute("keyword", keyword);
		return "edit";
	}
}
